#encoding: utf-8
require 'net/http'
require 'iconv'
require 'date'

class JillionStock < ActiveRecord::Base
  before_validation :init_create
  validates :stock_id, :presence => {:message => "不能为空"}
  validates :stock_name, :presence => {:message => "股票代码错误"}

  INFORMING_STATUS_NOT_INFORMED = '0' #没有通知
  RECOMMAND_STATUS_NOT_RECOMMANDED = '0'

  set_primary_key :stock_id

  def init_create
    Net::HTTP::Proxy("127.0.0.1", 8080).start('hq.sinajs.cn') { |http|
      response = http.get("/list=#{self.id}")
      stock_name = response.body.to_s[/(")[^"]*(")/]
      self.stock_name = Iconv.iconv("utf-8", "gbk", (stock_name[1, stock_name.length-2].split(',')[0]))[0] if !stock_name.blank?
    }
    self.informing_status = INFORMING_STATUS_NOT_INFORMED
    self.recommand_status = RECOMMAND_STATUS_NOT_RECOMMANDED
    self.add_time = DateTime.now
  end

  def analyse
    #判断当日股价是否为上涨

  end
end
